package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Validated
@Slf4j
@RestController
@RequestMapping("/v1/vehicle/")
@Api(tags = "02.车辆管理模块")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @ApiOperation("添加车辆功能")
    @ApiOperationSupport(order = 10)
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session) {
        log.debug("" + vehicleAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());
        int count = vehicleMapper.insertVehicle(vehicle);
        return count > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("list")
    @ApiOperation("车辆列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(VehicleListQuery vehicleListQuery, @ApiIgnore HttpSession session) {
        log.debug("" + vehicleListQuery);
        UserVO userVO = (UserVO) session.getAttribute("user");
        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        Long userId = userVO.getId();
        List<VehicleListVO> vehicleListVOList = vehicleMapper.vehicleList(brand, license, userId);
        log.debug("" + vehicleListVOList);
        return JsonResult.ok(vehicleListVOList);
    }

    @PostMapping("del/{id}")
    @ApiOperation("删除车辆")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id", value = "车辆编号", required = true)
    public JsonResult del(@Range(min = 201,max = 500,message = "车辆编号必须在201-500之间") @PathVariable Long id, @ApiIgnore HttpSession session) {
        log.debug("id: " + id);
        UserVO userVO = (UserVO) session.getAttribute("user");
        Long userId = userVO.getId();
        int count = vehicleMapper.deleteVehicleById(id, userId);
        return count > 0 ? JsonResult.ok() : new JsonResult(StatusCode.ILLEGAL_REQUEST);
    }

}
